package com.emm.thirdapp;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.leagsoft.emm.thirdparty.EMMToolsUtil;
import com.leagsoft.emm.thirdparty.callback.EMMProxyCallback;

import org.json.JSONObject;

import java.util.HashMap;


public class MainActivity extends Activity implements View.OnClickListener {

    private TextView tv_version, tv_web, tv_openapp, tv_initnet, tv_sso, tv_sanbox, tv_msg, tv_safecamera, tv_appJump;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        // 默认启动webview
        startActivity(new Intent(this, WebViewActivity.class));
    }

    private void initView() {
        tv_version = (TextView) findViewById(R.id.tv_version);
        tv_initnet = (TextView) findViewById(R.id.tv_initnet);
        tv_initnet.setOnClickListener(this);
        tv_web = (TextView) findViewById(R.id.tv_web);
        tv_web.setOnClickListener(this);
        tv_sso = (TextView) findViewById(R.id.tv_sso);
        tv_sso.setOnClickListener(this);
        tv_sanbox = (TextView) findViewById(R.id.tv_sanbox);
        tv_sanbox.setOnClickListener(this);
        tv_openapp = (TextView) findViewById(R.id.tv_openapp);
        tv_openapp.setOnClickListener(this);
        tv_msg = (TextView) findViewById(R.id.tv_msg);
        tv_msg.setOnClickListener(this);

        tv_safecamera = (TextView) findViewById(R.id.tv_safecamera);
        tv_safecamera.setOnClickListener(this);
        tv_appJump = findViewById(R.id.tv_appJump);
        tv_appJump.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        tv_version.setText("版本：v" + BuildConfig.VERSION_NAME);
        Log.w("MainActivity", "version:" + BuildConfig.VERSION_NAME);
    }

    @Override
    public void onClick(View v) {
        int viewid = v.getId();
        if (viewid == R.id.tv_initnet) {
            initNet();
        } else if (viewid == R.id.tv_sso) {
            String strsso = EMMToolsUtil.getSSOToken();
            Toast.makeText(this, "单点登录信息SSO：" + strsso, Toast.LENGTH_LONG).show();
        } else if (viewid == R.id.tv_web) {
            startActivity(new Intent(this, WebViewActivity.class));
        } else if (viewid == R.id.tv_safecamera) {
            startActivity(new Intent(this, SafeCameraActivity.class));
        } else if (viewid == R.id.tv_sanbox) {
            startActivity(new Intent(this, SanboxActivity.class));
        } else if (viewid == R.id.tv_openapp) {
            PackageManager packageManager = getPackageManager();
            Intent intent = new Intent();
            intent = packageManager.getLaunchIntentForPackage("com.eg.android.AlipayGphone");
            if (intent == null) {
                Toast.makeText(this, "未安装支付宝", Toast.LENGTH_LONG).show();
            } else {
                startActivity(intent);
            }
        } else if (viewid == R.id.tv_msg) {
            startActivity(new Intent(this, MsgDetailActivity.class));
        } else if (viewid == R.id.tv_appJump) {
            startActivity(new Intent(this, AppJumpActivity.class));
        }
    }

    /**
     * 初始代化代理
     */
    public void initNet() {
        EMMToolsUtil.initTunnel(this, new EMMProxyCallback() {
            @Override
            public void onSuccess() {
                Toast.makeText(MainActivity.this, "启动代理成功", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onError(int code, String msg) {
                Toast.makeText(MainActivity.this, "启动代理失败" + msg, Toast.LENGTH_LONG).show();
            }
        });
    }

}
